package com.csu.mining.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.csu.mining.R;
import com.csu.mining.adapters.MiningBottomAdapter;
import com.csu.mining.show.CarouselShow;

import java.util.ArrayList;

public class BaseActivity extends Activity {
    private ImageView actImg;
    private TextView actTitle;
    private ViewPager ViewPage_Detail;
    private LinearLayout point_detail;
    private Context context;
    private View view;
    //轮播图图片资源
    private final int[] viewpage_images = {R.mipmap.way1, R.mipmap.way2, R.mipmap.way3, R.mipmap.way4, R.mipmap.way5, R.mipmap.way6, R.mipmap.way7, R.mipmap.way8};

    private ArrayList<ImageView> viewpage_imageList;
    //判断是否自动滚动ViewPage
    private boolean isRunning = true;
    protected <T extends View> T fd (@IdRes int id){
        return findViewById(id);
    }

    protected void initNavBar(final boolean isMainActivity, String title){
        actImg = fd(R.id.nav_image);
        actTitle = fd(R.id.nav_title);
        if(!isMainActivity) {
            actImg.setImageResource(R.mipmap.back);
            Log.e("BaseAcvivity","not main activity,change image resource.");
        }
        actTitle.setText(title);
        actImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!isMainActivity) {
                    Log.e("BaseActivity","click back,will change.");
                    onBackPressed();
                }
            }
        });
    }

    protected void initBottonBar(String actName){
        RecyclerView mRvBottom;
        MiningBottomAdapter miningBottomAdapter;
        mRvBottom = fd(R.id.rv_bottom);
        mRvBottom.setLayoutManager(new GridLayoutManager(this,4));
        Log.e("MainActivity","will show bottom recyclerview");
        miningBottomAdapter = new MiningBottomAdapter(this,actName);
        mRvBottom.setAdapter(miningBottomAdapter);
    }

    public void Instantiation() {

        ViewPage_Detail = (ViewPager)findViewById(R.id.ViewPage_Detail);
        point_detail = (LinearLayout)findViewById(R.id.point_detail);

        //初始化图片资源
        viewpage_imageList = new ArrayList<ImageView>();
        for (int i : viewpage_images) {
            // 初始化图片资源
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(i);
            viewpage_imageList.add(imageView);

            // 添加指示小点
            ImageView point = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15,
                    15);
            params.rightMargin = 10;
            params.bottomMargin = 15;
            point.setLayoutParams(params);
            point.setBackgroundResource(R.mipmap.light_blue_point);
            if (i == R.mipmap.way1) {
                //默认聚焦在第一张
                point.setBackgroundResource(R.mipmap.blue_point);
                point.setEnabled(true);
            } else {
                point.setEnabled(false);
            }

            point_detail.addView(point);

        }
        //首页轮播
        CarouselShow carouselShow = new CarouselShow(context, viewpage_imageList);
        carouselShow.CarouselShow_Info_Detail((MainActivity) this);
        handler.sendEmptyMessageDelayed(0, 3000);
    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            // 执行滑动到下一个页面
            ViewPage_Detail.setCurrentItem(ViewPage_Detail.getCurrentItem() + 1);
            if (isRunning) {
                // 在发一个handler延时
                handler.sendEmptyMessageDelayed(0, 3000);
            }
        }
    };


    @Override
    public void onDestroy() {
        // 停止滚动
        isRunning = false;
        super.onDestroy();
    }
}
